ARC037 B - バウムテスト
提出
code: python
n, m = map(int, input().split())
graph = [[] for _ in range(n+1)]
for i in uv:
# 閉路が含まれる -> 3つ以上で循環
# 循環している ->
for i in graph:
for j in i:
解答
code: python
from collections import deque
n, m = list(map(int, input().split()))
U = []
V = []
for u, v in uv:
u -= 1
v -= 1
U.append(u)
V.append(v)
# print(U)
# print(V)
ans = 0
visited = set()
def dfs(i):
que = deque()
que.append(i)
flag = True
while que:
now = que.pop()
if now in visited: # 探索済み頂点をpopしたら閉路
flag = False
else:
visited.add(now)
for j in range(m):
# つながっていて未探索の頂点ならpush
if Uj == now and Vj not in visited: elif Vj == now and Uj not in visited: # 頂点iからの探索を終えたとき閉路があるかを返す
return flag
for l in range(n):
if l not in visited:
if dfs(l):
ans += 1
print(ans)
code: python
from collections import defaultdict
from collections import deque
n, m = list(map(int, input().split()))
d = defaultdict(list)
for u, v in uv:
# print(d)
# {1: 2, 2: 1, 3, 4, 3: 2, 4: 2, 5: 6, 6: 5, 7, 8, 7: 6, 8, 8: 6, 7} ans = 0
visited = set()
def dfs(i):
que = deque()
que.append(i)
flag = True
while que:
now = que.pop()
if now in visited: # 探索済み頂点をpopしたら閉路
flag = False
else:
visited.add(now)
if n not in visited:
que.append(n)
# 頂点iからの探索を終えたとき閉路があるかを返す
return flag
for l in range(1, n+1):
if l not in visited:
if dfs(l):
ans += 1
print(ans)
テーマ
メモ
提出
code: python
from collections import defaultdict
from collections import deque
n, m = list(map(int, input().split()))
d = defaultdict(list)
for u, v in uv:
# print(d)
# {1: 2, 2: 1, 3, 4, 3: 2, 4: 2, 5: 6, 6: 5, 7, 8, 7: 6, 8, 8: 6, 7} visited = set()
q = deque()
while q:
now = q.popleft()
for i in now:
if i in visited:
continue
else:
visited.add(i)
print(visited)